<?php session_start(); ?>
<?php include_once('misc/class_config.php'); ?>

<?php 
	include('access_control/page_access.php');
	
	setPreviousPage();
	declinePageIfNotLoggedIn();
	declinePageIfAdmin();
	
	$currentUser = new user();
	$currentUser->isLoggedIn();
	$currentUser->isTimedOut();
	
	$existingProduct = new product();
	$cartMessage = "";
	$actionSuccess = false;
	$actionMessage = "";
?>

<?php
	if($currentUser->loggedIn)
	{
		if(!$currentUser->timedOut)
		{
			try
			{
				if(empty($currentUser->creditcardNo))
				{
					include_once "controller/embedded_accountpaymentformcontroller.php";
				}
				else
				{
					include_once "controller/checkoutcontroller.php";
				}
			}
			catch(Exception $ex)
			{
				$cartMessage = $ex->getMessage();
			}
		}
	}
?>

<?php $pageTitle = "Checkout | Everything"; ?>
<?php include('layout/page_start.php'); ?>
<?php include('layout/page_header.php'); ?>
<?php include('layout/menu_bar.php'); ?>

<div id="main_content_div">

<?php
	echo $actionMessage;
	if($currentUser->loggedIn)
	{
		if($currentUser->timedOut)
		{
			include_once "layout/timeout.php";
			$currentUser->timeoutUser();
		}
		else
		{
			echo '<div class="page_message_div"><a>'.$cartMessage.'</a></div>';
			$userCart = new UserCartItem();
			$userCart->userId = $currentUser->id;
			$allCartItems = $userCart->getAllItems();
			if($allCartItems)
			{	
				if(empty($currentUser->creditcardNo))
				{
					include_once "layout/embedded_change_payment_form.php";
				}
				else
				{
					echo '<p style="text-align:center;">Current Credit Card Number: '.substr_replace($currentUser->creditcardNo,"************",0,12).'. If you want to change it, click <a href="account.php?action=editpaymentaccount" title="Change credit card">here</a>.</p>';
				}
				echo '<form method="post" action="cartcheckout.php">';
				include_once "layout/purchase_summary.php";
				echo '</form>';
			}
			else
			{
				$cartMessage = "Your shopping cart is empty.";
				echo '<div class="page_message_div"><p>'.$cartMessage.'</p></div>';
			}
		}
	}
?>
</div>

<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>
<?php setCurrentPage();?>